<?php

class MallGoodsController extends Controller
{
    /*
    * 获取商品列表
    *
    */
    public function actionGetgoodsList()
    {
        try {
            //参数验证

            //检测
            //$user_id = SessionService::getUserIDBySessionid();

            $material_id = Util::getParamNotEmpty("material_id");
            $sort_type = Util::getParam("sort_type",1);


            //获取列表
            $searchsql = Util::initSearchSql();

            $searchsql = $searchsql . " and material_id=" . $material_id;

            //排序
            $searchsql = MallGoodsService::getOrderStr($searchsql, $sort_type);

            //获取list
            $list = MallGoods::model()->findAll($searchsql);


            //返回数据
            Util::responseList(count($list), $list);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取店铺其他商品列表
    *
    */
    public function actionGetothergoodsList()
    {
        try {
            //参数验证

            //检测
            //$user_id = SessionService::getUserIDBySessionid();

            $goods_id = Util::getParamNotEmpty("goods_id");
            $sort_type = Util::getParamNotEmpty("sort_type");  //排序方式

            //验证商品是否存在
            $goodobj = MallGoods::model() -> findbyPK($goods_id);


            //获取列表
            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStrAdd($searchsql,"shop_id",$goodobj->shop_id);
            $searchsql = $searchsql . " and id <> " . $goods_id;

            //排序
            $searchsql = MallGoodsService::getOrderStr($searchsql, $sort_type);

            //获取list
            $list = MallGoods::model()->findAll($searchsql);


            //返回数据
            Util::responseList(count($list), $list);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取商品详情
    *
    */
    public function actionGetgoodsinfo()
    {
        try {
            //参数验证

            //检测
            //$user_id = SessionService::getUserIDBySessionid();

            $goods_id = Util::getParamNotEmpty("goods_id");

            $obj = MallGoods::model()->findbyPK($goods_id);

            $searchsql = Util::initSearchSql();

            $obj->specs_info = MallGoodsService::findspecsandattrs($searchsql, $goods_id);


            //返回数据
            Util::responseObj($obj);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 建立订单
    *
    */
    public function actionSetOrder()
    {

        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();


        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            Util::echolog("start");
            //参数验证
            $inputinfo = Util::getParamNotEmpty("inputinfo");
            $consignee = Util::getParamNotEmpty("consignee"); //收件人
            $moble = Util::getParamNotEmpty("moble");
            $address = Util::getParamNotEmpty("address");

            //生成运费
            $Shipping = 0;      //运费

            //$payby = Util::getParamNotEmpty("payby");        //支付方式(0平台金额，1快捷支付，2支付宝)

            MallGoodsService::createorder($user_id,$inputinfo,$consignee,$moble,$address,$Shipping);


            $transaction->commit();//事务结束
            //返回数据
            Util::responseErrorMsg(MsgConst::RETURN_SUCCESS);

        } catch (Exception $e) {

            $transaction->rollback();//回滚函数
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 设置支付方式
    *
    */
    public function actionSetpayby()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $obj_id = Util::getParamNotEmpty("obj_id");
            $payby = Util::getParamNotEmpty("payby");

            //验证是否存在此主订单
            $parentorder = MallOrderParent::model()->findbyPK($obj_id);

            //验证是否存在此支付方式
            if(!MallGoodsService::checkpayby($payby)){
                throw new Exception('不支持上述支付方式');
            }

            $parentorder->payby = $payby;

            Util::initObjData($parentorder);
            Util::saveObjData($parentorder);

            //返回成功信息
            Util::responseErrorMsg(MsgConst::RETURN_SUCCESS);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取用户订单列表
    *
    */
    public function actionGetOrderList()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //获取主订单列表
            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql, "user_id", $user_id);

            //排序
            $searchsql = $searchsql . " order by create_time  desc";

            //获取主订单list
            $parentorderlist = MallOrderParent::model()->findAll($searchsql);

            //根据主订单插入子订单
            foreach($parentorderlist as $value){

                $searchsql = Util::initSearchSql();

                $searchsql = Util::getSerarchStrAdd($searchsql, "parent_id", $value->id);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //获取子订单list
                $childorderlist = MallOrderChild::model()->findAll($searchsql);

                //根据子订单的goods_id插入对应的规格详情
                foreach($childorderlist as $value2){

                    $searchsql = Util::initSearchSql();
                    $value2->detail_info = MallGoodsService::findspecsandattrs($searchsql, $value2->goods_id);
                }

                $value->child_order = $childorderlist;
            }


                //返回数据
                Util::responseList(count($parentorderlist), $parentorderlist);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取店铺列表
    *
    */
    public function actiongetShopInfo()
    {
        try {
            //参数验证

            //检测
            //$user_id = SessionService::getUserIDBySessionid();
            $shop_id = Util::getParamNotEmpty("shop_id");


            //店铺信息
            $shop_info = MallShop::model()->findByPK($shop_id);
            Util::checkExistAndNotDelete($shop_info);



            Util::responseObj($shop_info);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取店铺内所有商品信息
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
           // $user_id = SessionService::getUserIDBySessionid();
            $shop_id = Util::getParamNotEmpty("shop_id");


            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStrAdd($searchsql,"shop_id",$shop_id);


            //获取count
            $totalcount = MallGoods ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallGoods::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }








}